/**
 * Copyright (c) 2014, 2017, Oracle and/or its affiliates.
 * The Universal Permissive License (UPL), Version 1.0
 */
"use strict";
/*
 Copyright 2013 jQuery Foundation and other contributors
 Released under the MIT license.
 http://jquery.org/license
 Copyright 2013 jQuery Foundation and other contributors
 Released under the MIT license.
 http://jquery.org/license
*/
define(["ojs/ojcore","jquery","ojs/ojdatasource-common"],function(a){a.Ba=function(g){if(!(g instanceof a.W))throw Error(a.W.od._ERR_DATA_INVALID_TYPE_SUMMARY+"\n"+a.W.od._ERR_DATA_INVALID_TYPE_DETAIL);this.Md=g;this.oa=0;this.Qk=-1;this.vq=[];this.Init()};o_("PagingTableDataSource",a.Ba,a);a.b.sa(a.Ba,a.W,"oj.PagingTableDataSource");a.Ba.prototype.Init=function(){a.Ba.u.Init.call(this)};a.b.g("PagingTableDataSource.prototype.Init",{Init:a.Ba.prototype.Init});a.Ba.prototype.getWrappedDataSource=function(){return this.Md};
a.b.g("PagingTableDataSource.prototype.getWrappedDataSource",{getWrappedDataSource:a.Ba.prototype.getWrappedDataSource});a.Ba.prototype.getPage=function(){return"loadMore"==this.YC?0:this.KJ()};a.b.g("PagingTableDataSource.prototype.getPage",{getPage:a.Ba.prototype.getPage});a.Ba.prototype.setPage=function(g,b){b=b||{};g=parseInt(g,10);try{a.Ba.u.handleEvent.call(this,a.bd.O.BEFOREPAGE,{page:g,previousPage:this.KJ()})}catch(c){return Promise.reject(null)}var d=this.KJ();this.wb=null!=b.pageSize?b.pageSize:
this.wb;b.pageSize=this.wb;b.startIndex=g*this.wb;this.oa=null==b.startIndex?this.oa:b.startIndex;this.YC="page";var e=this;return new Promise(function(c,g){0<e.wb?e.Md.fetch(b).then(function(b){b.startIndex=0;0<b.data.length?e.yw(e.oa+b.data.length-1,!0):e.yw(-1,!0);a.Ba.u.handleEvent.call(e,a.bd.O.PAGE,{page:e.KJ(),previousPage:d});c(null)},function(){e.oa=d*e.wb;g(null)}):c(null)})};a.b.g("PagingTableDataSource.prototype.setPage",{setPage:a.Ba.prototype.setPage});a.Ba.prototype.getStartItemIndex=
function(){return"loadMore"==this.YC?0:this.oa};a.b.g("PagingTableDataSource.prototype.getStartItemIndex",{getStartItemIndex:a.Ba.prototype.getStartItemIndex});a.Ba.prototype.getEndItemIndex=function(){return this.Qk};a.b.g("PagingTableDataSource.prototype.getEndItemIndex",{getEndItemIndex:a.Ba.prototype.getEndItemIndex});a.Ba.prototype.getPageCount=function(){var a=this.totalSize();return-1==a?-1:Math.ceil(a/this.wb)};a.b.g("PagingTableDataSource.prototype.getPageCount",{getPageCount:a.Ba.prototype.getPageCount});
a.Ba.prototype.at=function(a,b){return this.Md.at(a,b)};a.b.g("PagingTableDataSource.prototype.at",{at:a.Ba.prototype.at});a.Ba.prototype.fetch=function(a){a=a||{};if(null==a.startIndex)return this.setPage(this.getPage());this.YC="loadMore";this.oa=null==a.startIndex?this.oa:a.startIndex;var b=null==a.pageSize?this.wb:a.pageSize;null==this.wb&&(this.wb=b);a.pageSize=b;a.startIndex=this.oa;var c=this;return new Promise(function(d,e){0<b?c.Md.fetch(a).then(function(a){0<a.data.length?c.yw(c.oa+a.data.length-
1,!0):c.yw(-1,!0);d(a)},function(a){e(a)}):d(null)})};a.b.g("PagingTableDataSource.prototype.fetch",{fetch:a.Ba.prototype.fetch});a.Ba.prototype.get=function(a,b){return this.Md.get(a,b)};a.b.g("PagingTableDataSource.prototype.get",{get:a.Ba.prototype.get});a.Ba.prototype.getCapability=function(a){return this.Md.getCapability(a)};a.b.g("PagingTableDataSource.prototype.getCapability",{getCapability:a.Ba.prototype.getCapability});a.Ba.prototype.on=function(g,b){var c=this,d=this.Md;if(g==a.W.O.SYNC){var e=
function(a){c.jya(a,b)};this.vq.push({eventType:g,eventHandler:b,wrappedEventHandler:e});d.on(g,e)}else if(g==a.W.O.ADD||g==a.W.O.REMOVE||g==a.W.O.CHANGE)e=function(a){c.Xxa(a,b)},this.vq.push({eventType:g,eventHandler:b,wrappedEventHandler:e}),d.on(g,e);else if(g==a.W.O.REFRESH||g==a.W.O.RESET)e=function(a){c.oa=0;b(a)},this.vq.push({eventType:g,eventHandler:b,wrappedEventHandler:e}),d.on(g,e);else if(g==a.bd.O.PAGE||g==a.bd.O.BEFOREPAGE||g==a.bd.O.PAGECOUNT)a.Ba.u.on.call(this,g,b);else d.on(g,
b)};a.b.g("PagingTableDataSource.prototype.on",{on:a.Ba.prototype.on});a.Ba.prototype.off=function(g,b){g!=a.bd.O.PAGE&&g!=a.bd.O.PAGECOUNT||a.Ba.u.off.call(this,g,b);var c=this.Md;if(null!=this.vq){var d=this.vq.length,e;for(e=0;e<d;e++)if(this.vq[e].eventType==g&&this.vq[e].eventHandler==b){c.off(g,this.vq[e].wrappedEventHandler);this.vq.splice(e,1);break}}c.off(g,b)};a.b.g("PagingTableDataSource.prototype.off",{off:a.Ba.prototype.off});a.Ba.prototype.sort=function(a){null==a?a=this.sortCriteria:
this.sortCriteria=a;return this.Md.sort(a)};a.b.g("PagingTableDataSource.prototype.sort",{sort:a.Ba.prototype.sort});a.Ba.prototype.totalSize=function(){return this.Md.totalSize()};a.b.g("PagingTableDataSource.prototype.totalSize",{totalSize:a.Ba.prototype.totalSize});a.Ba.prototype.totalSizeConfidence=function(){return this.Md.totalSizeConfidence()};a.b.g("PagingTableDataSource.prototype.totalSizeConfidence",{totalSizeConfidence:a.Ba.prototype.totalSizeConfidence});a.Ba.prototype.KJ=function(){return 0<
this.wb?Math.floor(this.oa/this.wb):0};a.Ba.prototype.Xxa=function(a,b){var c=[],d;for(d=0;d<a.indexes.length;d++){var e=a.indexes[d];void 0!==e&&("page"==this.YC&&(e-=this.oa),(0>e||e>=this.oa+this.wb)&&c.push(d))}if(0<c.length)for(c.sort(function(a,b){return a-b}),d=c.length-1;0<=d;d--)a.data.splice(c[d],1),a.indexes.splice(c[d],1),a.keys.splice(c[d],1);0<a.indexes.length&&this.yw(a.indexes[a.indexes.length-1],!1);a.startIndex=this.oa;b(a)};a.Ba.prototype.jya=function(g,b){g.startIndex!=this.oa&&
(this.oa=g.startIndex);0<g.data.length?this.yw(g.startIndex+g.data.length-1,!0):this.yw(-1,!0);if("page"==this.YC){var c={};a.Uc.yf(c,g);c.startIndex=0;b(c)}else b(g)};a.Ba.prototype.yw=function(a,b){this.Qk=b?a:a>this.Qk?a:this.Qk;var c=this.totalSize();0<c&&(this.Qk=this.Qk>c-1?c-1:this.Qk)};a.Ba.O={ADD:"add",REMOVE:"remove",RESET:"reset",SYNC:"sync",REFRESH:"refresh",SORT:"sort"};o_("PagingTableDataSource.EventType",a.Ba.O,a);a.bd=function(){};o_("PagingModel",a.bd,a);a.bd.prototype.getPage=function(){};
a.b.g("PagingModel.prototype.getPage",{getPage:a.bd.prototype.getPage});a.bd.prototype.setPage=function(){};a.b.g("PagingModel.prototype.setPage",{setPage:a.bd.prototype.setPage});a.bd.prototype.getStartItemIndex=function(){};a.b.g("PagingModel.prototype.getStartItemIndex",{getStartItemIndex:a.bd.prototype.getStartItemIndex});a.bd.prototype.getEndItemIndex=function(){};a.b.g("PagingModel.prototype.getEndItemIndex",{getEndItemIndex:a.bd.prototype.getEndItemIndex});a.bd.prototype.getPageCount=function(){};
a.b.g("PagingModel.prototype.getPageCount",{getPageCount:a.bd.prototype.getPageCount});a.bd.prototype.totalSize=function(){};a.b.g("PagingModel.prototype.totalSize",{totalSize:a.bd.prototype.totalSize});a.bd.prototype.totalSizeConfidence=function(){};a.b.g("PagingModel.prototype.totalSizeConfidence",{totalSizeConfidence:a.bd.prototype.totalSizeConfidence});a.bd.O={BEFOREPAGE:"beforePage",PAGE:"page",PAGECOUNT:"pageCount"};o_("PagingModel.EventType",a.bd.O,a)});